package com.ruicar.afs.cloud.account.common.processor;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import java.io.Serializable;

/**
 * @author infinity
 * @version 1.0
 * @date 2020/7/22 10:23
 */
public class IRequest<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private String code = "0000";
    private String msg = "success";
    @JsonInclude(Include.NON_NULL)
    private T data;

    public IRequest() {
    }

    public IRequest(T data) {
        this.data = data;
    }

    public IRequest(T data, String msg) {
        this.data = data;
        this.msg = msg;
    }

    public IRequest(Throwable e) {
        this.msg = e.getMessage();
        this.code = "9999";
    }

    public static IRequest fail(String msg) {
        return (new IRequest(false, msg)).setCode("9999");
    }

    public static <T> IRequest success(T data) {
        return (new IRequest()).setData(data);
    }

    public static <T> IRequest.IResponseBuilder<T> builder() {
        return new IRequest.IResponseBuilder();
    }

    public String toString() {
        return "IResponse(code=" + this.getCode() + ", msg=" + this.getMsg() + ", data=" + this.getData() + ")";
    }

    public IRequest(String code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public String getCode() {
        return this.code;
    }

    public IRequest<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public String getMsg() {
        return this.msg;
    }

    public IRequest<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public T getData() {
        return this.data;
    }

    public IRequest<T> setData(T data) {
        this.data = data;
        return this;
    }

    public static class IResponseBuilder<T> {
        private String code;
        private String msg;
        private T data;

        IResponseBuilder() {
        }

        public IRequest.IResponseBuilder<T> code(String code) {
            this.code = code;
            return this;
        }

        public IRequest.IResponseBuilder<T> msg(String msg) {
            this.msg = msg;
            return this;
        }

        public IRequest.IResponseBuilder<T> data(T data) {
            this.data = data;
            return this;
        }

        public IRequest<T> build() {
            return new IRequest(this.code, this.msg, this.data);
        }

        public String toString() {
            return "IRequest.IResponseBuilder(code=" + this.code + ", msg=" + this.msg + ", data=" + this.data + ")";
        }
    }
}

